package BlogMybatis.blog.service.impl;

import BlogMybatis.blog.pojo.Tag;
import BlogMybatis.blog.mapper.TagMapper;
import BlogMybatis.blog.service.TagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Brandt
 * @since 2020-06-07
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Autowired
    private TagMapper tagMapper;


    //从tagIds字符串中获取id，根据id获取tag标签集合
    @Override
    public List<Tag> getTagByString(String ids) {
        List<Tag> tags = new ArrayList<>();
        List<Long> longs = convertToList(ids);
        for (Long long1 : longs) {
            tags.add(tagMapper.selectById(long1));
        }
        return tags;
    }

    @Override
    public List<Tag> getIndexTag() {
        return tagMapper.getIndexTag();
    }

    //把前端的tagIds字符串转换成list集合
    private List<Long> convertToList(String ids){
        List<Long> list = new ArrayList<>();
        if(!"".equals(ids) && ids != null){
            String[] idarrary = ids.split(",");
            for (int i = 0; i < idarrary.length; i++) {
                list.add(new Long(idarrary[i]));
            }
        }
        return list;
    }
}
